package com.blinddate.controller;

import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.result.Result;
import com.blinddate.pojo.vo.UserInfVO;
import com.blinddate.pojo.vo.UserLoginVO;
import com.blinddate.properties.JwtProperties;
import com.blinddate.service.UserInfService;
import com.blinddate.service.UserService;
import com.blinddate.utils.JwtUtil;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserInfService userInfService;
    @ApiOperation("用户微信登录")
    @PostMapping("/login")
    public Result wxlogin(String code){
        log.info("微信登录 用户:{}",code);
        User user=userService.wxlogin(code);
        UserLoginVO userLoginVO =new UserLoginVO();
        BeanUtils.copyProperties(user,userLoginVO);
        log.info("{}",user);
        //为微信用户生成jwt令牌
        Map<String,Object> cliams=new HashMap<>();
        cliams.put("userid",user.getId());
        String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),cliams);
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);
    }


    @ApiOperation("获取用户信息列表")
    @GetMapping("/userInf")
    public Result<List<UserInfVO>> userInf(Long userId,Integer gender){
        List<UserInfVO> userInfList = new ArrayList<>();
        userInfList = userInfService.getUserList(userId,gender);
        if (userInfList !=null ){
            return Result.success(userInfList);
        }
        return Result.error("请稍后重试");
    }

    @ApiOperation("获取指定用户信息")
    @GetMapping("/useroneInf")
    public Result<UserInfVO> useroneInf(Long userId, Long getId){
        UserInfVO oneUser = userInfService.getOneUser(userId, getId);
        if (oneUser!=null){
            return Result.success(oneUser);
        }
        return Result.error("用户id不存在");
    }



    @ApiOperation("提交信息卡")
    @PostMapping("/sub_user_inf")
    public Result subUserInf(@RequestBody UserInf userInf){
        Integer integer = userInfService.uploaduserInf(userInf);
        if (integer == 1){
            return Result.success("上传成功");
        }
        return Result.error("上传失败");
    }

    @ApiOperation("更新用户信息卡")
    @PostMapping("/updat_user_inf")
    @CacheEvict(cacheNames = "useInfCahe",key = "#userInf.userId")
    public Result updateUserInf(@RequestBody UserInf userInf){
        boolean b = userInfService.updateById(userInf);
        if (b){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation("获取自己的信息")
    @PostMapping("/get_self_inf")
    public Result<UserInf> getSelfInf(Long userId){
        UserInf userInf = userInfService.getUserInf(userId);
        return Result.success(userInf);
    }
    @ApiOperation("解锁别人的具体信息")
    @PostMapping("/unlock")
    public Result unlock(Long id){
        UserInfVO userInfVO=userInfService.unlock(id);
        if(userInfVO==null){
            return Result.error("无可用的查看次数！");
        }
        return Result.success(userInfVO);
    }
}
